package org.justdroid.justdroid;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.Ringtone;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class Utils {
	public static HashMap<String, String> callingDetails = null;
	public static int off_hook = 0;
	public static int ringing = 0;
	public static int loudspeaker = 0;
	public static String CALL_ENDED = "org.justdroid.justdroid.CallStateService.CALL_ENDED";
	public static String INCOMING_CALL = "org.justdroid.justdroid.CallStateService.INCOMING_CALL";
	public static String END_CALL = "org.justdroid.justdroid.CallStateService.END_CALL";
	public static ArrayList<String> callers = new ArrayList<String>();
	public static ArrayList<String> numbers = new ArrayList<String>();
	@SuppressWarnings("serial")
	public static ArrayList<String> colorNames = new ArrayList<String>() {
		{
			add("Cyan");
			add("Black");
			add("Blue");
			add("Magenta");
			add("Grey");
			add("Green");
			add("Red");
			add("White");
			add("Yellow");
		}
	};
	@SuppressWarnings("serial")
	public static ArrayList<String> numberType = new ArrayList<String>() {
		{
			add("Mobile");
			add("Home");
			add("Work");
			add("Work Mobile");
			add("Home Fax");
			add("Pager");
			add("Other");
		}
	};
	@SuppressWarnings("serial")
	public static ArrayList<String> fontType = new ArrayList<String>() {
		{
			add("None");
			add("Serif");
			add("Monospace");
		}
	};
	
	public static final String THREEG = "3G";
	public static final String TWOG = "2G";
	public static final String WIFI = "Wi Fi";
	public static final int OUTGOING = 1;
	public static final int INCOMING = 2;
	public static final int NONE = 0;
	public static final int SERIF = 1;
	public static final int MONOSPACE = 2;
	
	public static Ringtone ringtone;
	public static AudioManager audioManager;
	
	@SuppressWarnings("deprecation")
	public static void iterateToApplyColor(View view, int bgColor, int fgColor) {
		if(view instanceof ViewGroup) {
			for(int index = 0; index < ((ViewGroup)view).getChildCount(); index++)
				iterateToApplyColor(((ViewGroup)view).getChildAt(index), bgColor, fgColor);
		}
		else {
			if(view.getClass() == Button.class && view.getId() != R.id.btnNavigationBack && view.getId() != R.id.btnNavigationHome) {
					if(bgColor != view.getContext().getResources().getColor(R.color.card_background_regular)) {
				
						((Button)view).setBackgroundColor(bgColor);
				
					}
					else {
						((Button)view).setBackgroundDrawable(((Button)view).getContext().getResources().getDrawable(R.drawable.card));
					}
				 	((Button)view).setTextColor(fgColor);
			}
			else if(view.getClass() == TextView.class && !(((TextView)view).getText().toString().trim().equals(""))) {
				if(bgColor != view.getContext().getResources().getColor(R.color.card_background_regular)) {
					((TextView)view).setBackgroundColor(bgColor);
				}
				((TextView)view).setTextColor(fgColor);
			}
			else if(view.getClass() == RadioButton.class && !(((RadioButton)view).getText().toString().trim().equals(""))) {
				if(bgColor != view.getContext().getResources().getColor(R.color.card_background_regular)) {
					((RadioButton)view).setBackgroundColor(bgColor);
				}
				((RadioButton)view).setTextColor(fgColor);
			}
		}
	}

	public static void iterateToApplyFontSize(View v, float fontSize) {
		if(v instanceof ViewGroup) {
			for(int index = 0; index < ((ViewGroup)v).getChildCount(); index++)
				iterateToApplyFontSize(((ViewGroup)v).getChildAt(index), fontSize);
		}
		else {
			if(v.getClass() == Button.class && v.getId() != R.id.btnNavigationBack && v.getId() != R.id.btnNavigationHome) {
					((Button)v).setTextSize(fontSize);
			}
			else if(v.getClass() == TextView.class && !(((TextView)v).getText().toString().trim().equals(""))) {
				((TextView)v).setTextSize(fontSize);
			}
			else if(v.getClass() == RadioButton.class && !(((RadioButton)v).getText().toString().trim().equals(""))) {
				((RadioButton)v).setTextSize(fontSize);
			}
		}
	}
	
	public static void iterateToApplyFontType(View v, int fontType) {
		if(v instanceof ViewGroup) {
			for(int index = 0; index < ((ViewGroup)v).getChildCount(); index++)
				iterateToApplyFontType(((ViewGroup)v).getChildAt(index), fontType);
		}
		else {
			if(v.getClass() == Button.class && v.getId() != R.id.btnNavigationBack && v.getId() != R.id.btnNavigationHome) {
				switch(fontType) {
					case NONE:
						((Button)v).setTypeface(null, Typeface.BOLD);
						break;
					case SERIF:
						((Button)v).setTypeface(Typeface.SERIF);
						break;
					case MONOSPACE:
						((Button)v).setTypeface(Typeface.MONOSPACE);
						break;
				}
			}
			else if(v.getClass() == TextView.class && !(((TextView)v).getText().toString().trim().equals(""))) {
				switch(fontType) {
				case NONE:
					((TextView)v).setTypeface(null, Typeface.BOLD);
					break;
				case SERIF:
					((TextView)v).setTypeface(Typeface.SERIF);
					break;
				case MONOSPACE:
					((TextView)v).setTypeface(Typeface.MONOSPACE);
					break;
				}
			}
			else if(v.getClass() == RadioButton.class && !(((RadioButton)v).getText().toString().trim().equals(""))) {
				switch(fontType) {
				case NONE:
					((RadioButton)v).setTypeface(null, Typeface.BOLD);
					break;
				case SERIF:
					((RadioButton)v).setTypeface(Typeface.SERIF);
					break;
				case MONOSPACE:
					((RadioButton)v).setTypeface(Typeface.MONOSPACE);
					break;
				}
			}
		}
	}
	
	
	public static void applyFontColorChanges(Context context, LinearLayout layout) {
		//get the values in SharedPreferences
				SharedPreferences preferences = context.getSharedPreferences(context.getResources().getString(R.string.color), 0);
				if(preferences.getInt("bgcolor", 0) != 0 || preferences.getInt("fgcolor", 0) != 0) {
					int bgColor = preferences.getInt("bgcolor", 0);
					int fgColor = preferences.getInt("fgcolor", 0);
					try{
						context.getResources().getResourceName(bgColor);
						bgColor = context.getResources().getColor(bgColor);
					}
					catch(NotFoundException nfe) {
						bgColor = context.getResources().getColor(R.color.card_background_regular);
					}
					try{
						context.getResources().getResourceName(fgColor);
						fgColor = context.getResources().getColor(fgColor);
					}
					catch(NotFoundException nfe) {
						fgColor = context.getResources().getColor(R.color.card_textcolor_regular);
					}
					Utils.iterateToApplyColor(layout, bgColor, fgColor);
				}
				else {
					Utils.iterateToApplyColor(layout, context.getResources().getColor(R.color.card_background_regular), context.getResources().getColor(R.color.card_textcolor_regular));
				}
	}
	
	public static void applyFontSizeChanges(Context context, LinearLayout layout) {
				//get the values in SharedPreferences
				SharedPreferences preferences = context.getSharedPreferences(context.getResources().getString(R.string.size), 0);
				if(preferences.getFloat("size", 0) != 0) {
					float fontSize = preferences.getFloat("size", 0);
					Utils.iterateToApplyFontSize(layout, fontSize);
				}
				else {
					Utils.iterateToApplyFontSize(layout, context.getResources().getDimension(R.dimen.card_textsize_regular));
				}
			}

	public static void applyFontTypeChanges(Context context, LinearLayout layout) {
		//get the values in SharedPreferences
		SharedPreferences preferences = context.getSharedPreferences(context.getResources().getString(R.string.fonttype), 0);
		if(preferences.getInt("typeface", -1) != -1) {
			Utils.iterateToApplyFontType(layout, preferences.getInt("typeface", -1));
		}
		else {
			Utils.iterateToApplyFontType(layout, 0);
		}
	}
	
	public static boolean isAccessibilityEnabled(Context context) {
		AccessibilityManager am = (AccessibilityManager)context.getSystemService(Context.ACCESSIBILITY_SERVICE);
		return am.isEnabled();
	}
}
